<?php
class Index extends Action {
	private $sort;
	private $sorts = array ('name' => '按名称', 'ext' => '按类型', 'size' => '按大小', 'mtime' => '按时间' );
	private $totalDir = 0;
	private $totalFile = 0;
	private $totalSize = 0;
	private $exts = array ('txt' => 'txt', 'xml' => 'xml', 'doc' => 'doc', 'docx' => 'doc', 'ppt' => 'doc', 'pptx' => 'doc', 'xls' => 'doc', 'xlsx' => 'doc', 'dir' => 'dir', 'php' => 'php', 'js' => 'js', 'css' => 'css', 'tpl' => 'html', 'html' => 'html', 'htm' => 'html', 'pdf' => 'pdf', 'jpg' => 'img', 'jpeg' => 'img', 'png' => 'img', 'gif' => 'img', 'zip' => 'zip', 'rar' => 'zip', 'gzip' => 'zip', 'gz' => 'zip', 'biz2' => 'zip' );
	/* (non-PHPdoc)
	 * @see Action::execute()
	 */
	public function execute($params) {
		PluginManager::enabled ( 'cn.usephp.filemgr');
		$me = check_login ( ADMIN_LOGIN_PAGE );		
		$data = array ();
		$data['url'] = $this->url;
		$view = rqst ( 'view', 'list' );
		if ($view != 'list') {
			$view = 'icon';
		}
		$this->sort = rqst ( 'sort', 'name' );
		if (! isset ( $this->sorts [$this->sort] )) {
			$this->sort = 'name';
		}
		$data ['sortname'] = $this->sorts [$this->sort];
		$dir = rqst ( "dir", '' );
		$dir = trim ( $dir, './\\' );
		$dir = str_replace ( '../', '', $dir );
		$data ['dirs'] = $this->splitDir ( $dir );
		$data ['view'] = $view;
		$data ['view_tpl'] = 'core/views/files/' . $view . '_view.tpl';
		$data ['cwd'] = $dir;
		$data ['files'] = $this->loadFiles ( $dir );
		$data ['totalDir'] = $this->totalDir;
		$data ['totalFile'] = $this->totalFile;
		$data ['totalSize'] = readable_size ( $this->totalSize );
		//bind ( 'get_file_options', array ($this, 'get_file_options' ), 1, 3 );
		return new SmartyView ( $data, 'files/Index' );
	}
	public function loadFiles($dir) {
		$cdir = APP_PATH . $dir;
		if (! is_dir ( $cdir )) {
			return array ();
		}
		$hd = opendir ( $cdir );
		if (! $hd) {
			return array ();
		}
		$files = array ();
		$dirs = array ();
		while ( ($f = readdir ( $hd )) !== false ) {
			if ($f == '.' || $f == '..' || $f == '.svn') {
				continue;
			}
			$file = $cdir . '/' . $f;
			$f = thefilename ( $f );
			if (is_dir ( $file )) { //是目录
				$stat = @stat ( $file );
				$dirs [] = array ('isdir' => true, 'name' => $f, 'file' => $dir . '/' . $f, 'ext' => 'dir', 'size' => "", 'mtime' => date ( 'Y-m-d H:i:s', $stat ['mtime'] ) );
				$this->totalDir += 1;
			} else {
				$this->totalFile += 1;
				$ext = strtolower ( pathinfo ( $file, PATHINFO_EXTENSION ) );
				$ext = isset ( $this->exts [$ext] ) ? $this->exts [$ext] : 'unkown';
				$stat = @stat ( $file );
				if ($stat) {
					$this->totalSize += $stat ['size'];
					$files [] = array ('isdir' => false, 'name' => $f, 'file' => $dir . '/' . $f, 'ext' => $ext, 'size1' => $stat ['size'], 'size' => readable_size ( $stat ['size'] ), 'mtime' => date ( 'Y-m-d H:i:s', $stat ['mtime'] ) );
				} else {
					$files [] = array ('isdir' => false, 'name' => $f, 'file' => $dir . '/' . $f, 'ext' => $ext, 'size1' => 0, 'size' => '未知', 'mtime' => '' );
				}
			}
		}
		closedir ( $hd );
		usort ( $dirs, array ($this, 'sortDir' ) );
		usort ( $files, array ($this, 'sortFile' ) );
		return array_merge ( $dirs, $files );
	}
	public function sortFile($a, $b) {
		if ($this->sort == 'name') {
			return strcmp ( $a ['name'], $b ['name'] );
		} else if ($this->sort == 'ext') {
			return strcmp ( $a ['ext'], $b ['ext'] );
		} else if ($this->sort == 'size') {
			return ($a ['size1'] < $b ['size1']) ? - 1 : 1;
		} else if ($this->sort == 'mtime') {
			return strcmp ( $a ['mtime'], $b ['mtime'] );
		}
	}
	public function sortDir($a, $b) {
		return strcmp ( $a ['name'], $b ['name'] );
	}
	
	public function get_file_options($ops, $file, $type) {
	
	}
	//分隔目录
	private function splitDir($dir) {
		if (empty ( $dir )) {
			return array ();
		} else {
			$dirs = explode ( '/', $dir );
			$rst = array ();
			$dstr = '';
			foreach ( $dirs as $d ) {
				$dstr .= $d . '/';
				$rst [] = array ($d, $dstr );
			}
			return $rst;
		}
	}
}